package cn.jinjimi.app.custom.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.LocalEvent.CustomLasteFollowRefreshEvent;
import cn.jinjimi.app.custom.event.CustomChangeEvent;
import cn.jinjimi.app.custom.event.CustomLastedFollowsEvent;
import cn.jinjimi.app.custom.ui.adapter.CustomLastFollowListAdapter;
import cn.jinjimi.app.interfaces.DataSetChangeListener;
import cn.jinjimi.app.main.dialog.LoadingDialog;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by Roy
 * Date: 17/1/9
 */

public class CustomLastedFollowListActivity extends ToolBarCommonActivity
        implements XListView.IXListViewListener, AdapterView.OnItemClickListener, DataSetChangeListener {

    private XListView mListView;
    private CustomLastFollowListAdapter mAdapter;
    private int page = Constants.DEFAULT_PAGE;

    public static void launch(Context startActivity) {
        if (startActivity == null)
            return;
        Intent intent = new Intent(startActivity, CustomLastedFollowListActivity.class);
        startActivity.startActivity(intent);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_custom_lasted_follow_list_layout);
        setTitle(R.string.title_lasted_follow_list);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mListView = (XListView) findViewById(R.id.listview);
        mListView.setTransparentBg();
        mListView.setPullLoadEnable(false);

        mAdapter = new CustomLastFollowListAdapter(CustomLastedFollowListActivity.this, this);
        mListView.setAdapter(mAdapter);

    }

    protected void initListener() {
        mListView.setXListViewListener(this);
        mListView.setOnItemClickListener(this);
    }

    protected void initData() {
        LoadingDialog.show(this, getString(R.string.loading_data));
        onRefresh();
    }

    @Override
    public void onRefresh() {
        CustomerController.getLastFollowList(Constants.DEFAULT_PAGE);
    }

    @Override
    public void onLoadMore() {
        CustomerController.getLastFollowList(page + 1);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int realP = position - mListView.getHeaderViewsCount();
        if (mAdapter.getCount() > realP && realP >= 0)
            CustomArchivesActivity.launch(CustomLastedFollowListActivity.this, mAdapter.getItem(realP).customerId);
    }

    /* 监听获取跟进记录列表的结果 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomLastedFollowsEvent(CustomLastedFollowsEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                if (event.data != null) {
                    if (event.data.data == null)
                        event.data.data = new ArrayList<>();

                    if (event.loadPage == Constants.DEFAULT_PAGE)
                        mAdapter.setItems(event.data.data);
                    else
                        mAdapter.addItems(event.data.data);

                    page = event.loadPage;
                    mListView.setPullLoadEnable(event.data.data.size() == Constants.PAGE_LIMIT_20);
                    mListView.setNoMore(event.data.data.size() < Constants.PAGE_LIMIT_20 && !mAdapter.isEmpty() && mAdapter.getCount() > 4, getString(R.string.nomore_loading));
                } else {
                    ToastUtils.toastMessage(CustomLastedFollowListActivity.this, getString(R.string.follow_get_info_fail));
                }

                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(CustomLastedFollowListActivity.this, getString(R.string.follow_get_info_fail));
                dataChange();
                break;
        }
        mListView.setRefreshTime(System.currentTimeMillis());
        mListView.stopRefresh();
        mListView.stopLoadMore();

        LoadingDialog.dispose();
    }

    /* 监听客户资料的变化 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomChangeEvent(CustomChangeEvent event) {
        onRefresh();
    }

    /* 监听跟进记录的变化 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomLasteFollowRefreshEvent(CustomLasteFollowRefreshEvent event) {
        onRefresh();
    }

    @Override
    public void dataChange() {
        findViewById(R.id.empty_view).setVisibility(mAdapter.isEmpty() ? View.VISIBLE : View.GONE);
    }
}
